package com.gangwangchina.android.contactlib.model;


import java.util.HashMap;
import java.util.Map;

import com.gangwangchina.android.contactlib.ContactsInterface;


import android.net.Uri;
import android.provider.ContactsContract.CommonDataKinds;

public class EmailAddress  extends DataElement{
	public static final String EMAIL = CommonDataKinds.Email.DATA;
	public static final String TYPE = CommonDataKinds.Email.TYPE;
	public static final String LABEL = CommonDataKinds.Email.LABEL;
	public static final String CONTENT_ITEM_TYPE = CommonDataKinds.Email.CONTENT_ITEM_TYPE;
	public static final Uri FILTER_SEARCH = CommonDataKinds.Email.CONTENT_FILTER_URI;
	
	public String email, label;
	public int type;

	/**
	 * Creates a new Email Address that is not (yet) present in the database
	 * @param email
	 * @param type
	 * @param label
	 * @return
	 */
	public static EmailAddress newEmailAddress(String email, int type, String label){
		return new EmailAddress(email, type, label);
	}
	
	/**
	 * Creates an object representation of an EmailAddress that is present in the database
	 * @param pcId
	 * @param email
	 * @param type
	 * @param label
	 * @return
	 */
	public static EmailAddress recoverEmailAddress(long pcId, String email, int type, String label){
		return new EmailAddress(pcId, email, type, label);
	}
	
	private EmailAddress(long pcId){
		super(pcId, CONTENT_ITEM_TYPE);
	}
	
	/**
	 * @param pcId the PersonContact the EmailAdress is linked to
	 * @param email the email address
	 * @param type the type of address
	 * @param label
	 */
	private EmailAddress(long pcId, String email, int type, String label){
		this(pcId);
		this.email = email;
		this.type = type;
		this.label = label;
	}
	
	private EmailAddress(String email, int type, String label){
		this(ContactsInterface.NO_ID, email, type, label);
	}
	
//	@Override
	public String toString(){		
//		return "{'mail':" + this.safeJson(email) + "type" + this.safeJson(type) + "}";		
		return "";
	}

	@Override
	public Map<String, Object> getDataColumns() {				
		Map<String, Object> datas = new HashMap<String, Object>();		
		datas.put(EMAIL, email);
		datas.put(TYPE, type);
		datas.put(LABEL, label);
		return datas;
	}
	
	@Override
	public boolean setProperty(String key, Object value){
		if(key.equals(EMAIL)) email = (String) value;
		else if(key.equals(TYPE)) type = new Integer((Integer) value);
		else if(key.equals(LABEL)) label = (String) value;
		else return super.setProperty(key, value);
		return true;
	}
	
	@Override
	public Object getProperty(String key){
		if(key.equals(EMAIL)) return email;
		else if(key.equals(TYPE)) return String.valueOf(type);
		else if(key.equals(LABEL)) return label;
		else return super.getProperty(key);
			
	}
}
